package com.ajz.security.web;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ajz.common.dto.Result;
import com.ajz.common.utils.Exceptions;
import com.ajz.common.web.BaseController;
import com.ajz.security.entity.Role;
import com.ajz.security.service.IRoleService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

@Controller
@RequestMapping("/security/role")
public class RoleController extends BaseController<Role, String> {

	@Autowired
	private IRoleService roleService;
    
	@Value("${ajz.security.role.queryRole}")
	private String queryRole;

	@Value("${ajz.security.role.addRole}")
	private String addRole;

	@Value("${ajz.security.role.editRole}")
	private String editRole;

	@Value("${ajz.security.role.deleteRole}")
	private String deleteRole;

	@Value("${ajz.security.role.batchDeleteRole}")
	private String batchDeleteRole;
	
	@RequestMapping(value = "/queryTree")
	@ResponseBody
	public Result queryTree(String keyWord) {
		
		try {
			List<Map<String, Object>> nodes = Lists.newArrayList();
			
			
			List<Role> roles = roleService.findByParentIsNull();
			for (Role role : roles) {
       		 getRolesCascade(role, nodes);
       	 }
			return  Result.successResult(nodes);

		} catch (Exception e) {
			String msg = Exceptions.getMessage(e);
			logger.error(msg, e);
			return Result.errorResult(msg);
		}
		
	}
//
//	@Override
//	@RequestMapping(value = "/save")
//	@ResponseBody
//	public Result save(Role entity) {
//		try {
//			if (!SecurityUtils.hasRole(this.getAddRole())) {
//				return Result.denyResult();
//			}
//
//			Role rol= roleService.save(entity);
//			if(rol != null){
//				 User user = userService.findByLoginName("admin");
//				 if(user!=null){
//					 List<Role> roles = user.getRoles();
//				        Assert.notNull(roles);
//				        roles.add(rol);
//				        user.setRoles(roles);
//				        userService.update(user); 
//				 }
//			}
//			
//			return Result.successResult();
//		} catch (Exception e) {
//			String msg = Exceptions.getMessage(e);
//			logger.error(msg, e);
//			return Result.errorResult(msg);
//		}
//
//	}
	private void getRolesCascade(Role role, List<Map<String, Object>> nodes) {
		
		Map<String, Object> roleData = Maps.newHashMap();
		roleData.put("tId", role.getId());
		roleData.put("id", role.getId());
		roleData.put("pId", role.getParent() == null ? null : role.getParent().getId());
		roleData.put("name", role.getRemark());

		nodes.add(roleData);
		
		for (Role childRole : role.getChildren()) {
			getRolesCascade(childRole, nodes);
		}
		
	}

	public String getQueryRole() {
		return queryRole;
	}

	public String getAddRole() {
		return addRole;
	}

	public String getEditRole() {
		return editRole;
	}

	public String getDeleteRole() {
		return deleteRole;
	}

	public String getBatchDeleteRole() {
		return batchDeleteRole;
	}
	
	
	
}
